<template>
  <h3>库区设置</h3>
  <!-- 搜索栏 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-form :model="reservoirQuery" inline>
        <el-form-item label="库区名称">
          <el-input v-model="reservoirQuery.reservoirName" placeholder="请输入仓库名称"/>
        </el-form-item>
        <el-form-item>
          <el-button @click="loadReservoirs" type="primary">搜索</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
  <!-- 工具栏 -->
  <el-row>
    <el-col :span="24">
      <el-button type="primary" @click="toAdd">新增</el-button>
      <el-button type="primary" @click="toEdit">修改</el-button>
      <el-button type="primary" @click="toDelete">删除</el-button>
    </el-col>
  </el-row>
  <!-- 数据表 -->
  <el-row style="margin-top: 10px">
    <el-col :span="24">
      <el-table
          :data="reservoirs"
          border
          stripe
      ><!-- stripe可以为表格添加斑马纹样式，默认false --><!--border表示给表格加上边框 -->
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="reservoirEncode" label="库区编码" width="200" align="center">
          <template #default="scope">
            <el-link @click="showDetail(scope.row.reservoirId)" type="primary">{{ scope.row.reservoirEncode }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="reservoirName" label="库区名称" width="250" align="center"/>
        <el-table-column prop="reservoirArea" label="面积" width="250" align="center"/>
        <el-table-column prop="reservoirRemark" label="备注" width="250" align="center"/>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button-group>
              <el-button size="small" type="primary" @click="bin(scope.row.reservoirId)">库位</el-button>
              <el-button size="small" type="primary" @click="editReservoir(scope.row.reservoirId)">修改</el-button>
              <el-button size="small" type="danger" @click="deleteReservoir(scope.row)">删除</el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
  </el-row>
<!--  &lt;!&ndash; 新增仓库设置弹窗 &ndash;&gt;-->
<!--  <setting-add :flag="addHouse" @cancelAdd="cancelAdd" @addComplete="loadHouses"/>-->
<!--  &lt;!&ndash; 编辑仓库设置弹窗 &ndash;&gt;-->
<!--  <setting-edit :house-id="editHouseId" @cancelEdit="cancelEdit" @editComplete="loadHouses"/>-->
<!--  &lt;!&ndash; 详情仓库设置弹窗 &ndash;&gt;-->
<!--  <setting-detail :house-id="detailHouseId" @cancelDetail="cancelDetail"/>-->
</template>

<script setup>
import {onMounted, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import reservoirRequest from "@/api/reservoir.js";
import {useRoute} from "vue-router";

let route = useRoute();
// 封装查询条件
let reservoirQuery = ref({});
// 封装查询的结果集合
let reservoirs = ref([]);
// 新增的addReservoir
let addReservoir = ref(-1);
// 编辑的editReservoirId
let editReservoirId = ref(-1);
// 详情的detailReservoirId
let detailReservoirId = ref(-1);


function showDetail(reservoirId) {
  detailReservoirId.value = reservoirId;
}
function cancelDetail(){
  detailReservoirId.value = -1;
}
function toAdd(){
  addReservoir.value = 1;
}
function cancelAdd(){
  addReservoir.value = -1;
}
function editReservoir(reservoirId){
  editReservoirId.value = reservoirId;
}
function cancelEdit() {
  editReservoirId.value = -1;
}
function loadReservoirs(){
  reservoirQuery.value.reservoirHouseId = route.params.id;
  reservoirRequest.loadReservoirs(reservoirQuery.value,res=>{
    reservoirs.value = res.data;
  })
}

function reset(){
  reservoirQuery.value.reservoirName = null;
  reservoirRequest.loadReservoirs(reservoirQuery.value,res=>{
    reservoirs.value = res.data;
  })
}
function deleteReservoir(reservoir){
  ElMessageBox.confirm(
      '您确定要将设备[' +reservoir.reservoirEncode + ']删除吗？',
      'Warning',
      {
        confirmButtonText: '是的',
        cancelButtonText: '点错了',
        type: 'warning',
      }
  ).then(() => {
    reservoirRequest.delete(reservoir.reservoirId, res => {
      ElMessage({
        type: "success",
        message: "操作完成"
      });
      loadReservoirs();
    })
  }).catch(() => {
  });
}

function bin(){

}

onMounted(()=>{
  loadReservoirs();
})
</script>

<style scoped>

</style>